package com.dalongxin.work.data;

import com.dalongxin.work.BaseApplication;
import com.dalongxin.work.Constants;
import com.dalongxin.work.utils.Utils;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@DatabaseTable(tableName="cloud_disk")
public class CloudDisk implements Serializable {

    @DatabaseField
    private String filePic;//	√	String	资源缩略图
    @DatabaseField
    private String parentId;//	√	String 所在目录id
    @DatabaseField
    private String fileId;//	√	String	资源id
    @DatabaseField
    private String fileName;//	√	String	资源名称
    @DatabaseField
    private String fileCreatTime;//	√	String	创建时间
    @DatabaseField
    private String fileType;//	√	String	0:图片 1:音频 2:视频 3:txt 4:pdf
    @DatabaseField
    private String fileSize;//	√	String	资源大小
    @DatabaseField
    private String createName;//	√	String	0:普通用户；1:创建者
    @DatabaseField
    private String createId;//	√	String	0:普通用户；1:创建者
    @DatabaseField
    private String fileUrl;//	√	String	下载地址
    @DatabaseField
    private String fileFix;//后缀

    private boolean isChecked;
    private boolean isFolder;
    private boolean isInTargetFolderList;
    private int downloadState; //下载状态

    public static final int DOWNLOADING = 1;
    public static final int PAUSING = 2;
    public static final int FINISHED = 3;

    public int getDownloadState() {
        return downloadState;
    }

    public void setDownloadState(int downloadState) {
        this.downloadState = downloadState;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }

    public boolean isFolder() {
        return isFolder;
    }

    public void setFolder(boolean folder) {
        isFolder = folder;
    }

    public boolean isInTargetFolderList() {
        return isInTargetFolderList;
    }

    public void setInTargetFolderList(boolean inTargetFolderList) {
        isInTargetFolderList = inTargetFolderList;
    }

    public String getFileFix() {
        return fileFix;
    }

    public void setFileFix(String fileFix) {
        this.fileFix = fileFix;
    }

    public String getFilePic() {
        return filePic;
    }

    public void setFilePic(String filePic) {
        this.filePic = filePic;
    }

    public String getFileId() {
        return fileId;
    }

    public void setFileId(String fileId) {
        this.fileId = fileId;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileCreatTime() {
        return fileCreatTime;
    }

    public void setFileCreatTime(String fileCreatTime) {
        this.fileCreatTime = fileCreatTime;
    }

    public String getFileType() {
        return fileType;
    }

    public void setFileType(String fileType) {
        this.fileType = fileType;
    }

    public String getFileSize() {
        return fileSize;
    }

    public void setFileSize(String fileSize) {
        this.fileSize = fileSize;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getCreateName() {
        return createName;
    }

    public void setCreateName(String createName) {
        this.createName = createName;
    }

    public String getCreateId() {
        return createId;
    }

    public void setCreateId(String createId) {
        this.createId = createId;
    }

    public String getFileUrl() {
        return fileUrl;
    }

    public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
    }

    public static CloudDisk parseFromJson(JSONObject jsonObj) {
        CloudDisk object = new CloudDisk();
        object.filePic = jsonObj.optString("filePic");
        object.fileId = jsonObj.optString("id");
        object.fileName = jsonObj.optString("name");
        object.fileCreatTime = jsonObj.optString("createTime");
        object.fileType = jsonObj.optString("fileType");
        object.fileSize = jsonObj.optString("fileSize");
        object.createId = jsonObj.optString("createId");
        object.createName = jsonObj.optString("createName");
        object.fileUrl = jsonObj.optString("path");
        object.fileFix = jsonObj.optString("fileFix");
        return object;
    }

    public static CloudDisk parseFolderFromJson(JSONObject jsonObj, boolean isInTargetFolderList) {
        CloudDisk object = new CloudDisk();
        object.filePic = jsonObj.optString("file_pic");
        object.fileId = jsonObj.optString("id");
        object.fileName = jsonObj.optString("name");
        object.fileCreatTime = jsonObj.optString("createTime");
        object.createId = jsonObj.optString("createId");
        object.createName = jsonObj.optString("createName");
        object.isFolder = true;
        object.isInTargetFolderList = isInTargetFolderList;
        return object;
    }

    public static List<CloudDisk> parseFromJsonList(JSONArray jsonArray) {
        List<CloudDisk> list = new ArrayList<CloudDisk>();
        if(jsonArray != null && jsonArray.length() > 0) {
            for(int i=0;i<jsonArray.length();i++) {
                CloudDisk item = parseFromJson(jsonArray.optJSONObject(i));
                if(item != null)list.add(item);
            }
        }
        return list;
    }

    /**
     *
     * @param jsonArray jsonArray
     * @param isInTargetFolderList 是否为目标文件夹列表中的文件夹
     * @return List
     */
    public static List<CloudDisk> parseFolderFromJsonList(JSONArray jsonArray, boolean isInTargetFolderList) {
        List<CloudDisk> list = new ArrayList<CloudDisk>();
        if(jsonArray != null && jsonArray.length() > 0) {
            for(int i=0;i<jsonArray.length();i++) {
                CloudDisk item = parseFolderFromJson(jsonArray.optJSONObject(i), isInTargetFolderList);
                if(item != null)list.add(item);
            }
        }
        return list;
    }

    /**
     * 获取本地保存的路径
     * @return
     */
    public String getLocalFullPath(){
        try{
            String userId = BaseApplication.getInstance().getDefaultAccount()==null?"0": BaseApplication.getInstance().getDefaultAccount().getId()+"";
            String[] tmp = fileUrl.split("/");
            String fileName = tmp[tmp.length - 1];
            String filePath = Utils.getSDPath() + Constants.FilePath + "/" + userId;
            String fullPath = filePath +"/"+ fileName;
            return fullPath;
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }
}
